package com.bdqn.business.academic.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.business.academic.dao.BooksMapper;
import com.bdqn.business.academic.dao.ChapterMapper;
import com.bdqn.business.academic.dao.CurriculumMapper;
import com.bdqn.business.academic.dao.SchoolMapper;
import com.bdqn.business.quality.dao.ClassesMapper;
import com.bdqn.entity.academic.Books;
import com.bdqn.entity.academic.Chapter;
import com.bdqn.entity.academic.Curriculum;
import com.bdqn.entity.academic.School;
import com.bdqn.entity.quality.Classes;
import com.bdqn.error.CommonException;
import com.bdqn.response.PageResult;
import com.bdqn.response.Result;
import com.bdqn.response.ResultCode;
import com.bdqn.utils.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SchoolService {

    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private CurriculumMapper curriculumMapper;

    @Autowired
    private BooksMapper booksMapper;

    @Autowired
    private ChapterMapper chapterMapper;

    @Autowired
    private ClassesMapper classesMapper;

    public Result getList(Map<String, Object> map) {
        IPage<School> iPage = new Page<>(Integer.parseInt(map.get("page").toString()),Integer.parseInt(map.get("size").toString()));
        QueryWrapper<School> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("sort_number");
        IPage<School> result = schoolMapper.selectPage(iPage, queryWrapper);
        return new Result(ResultCode.SUCCESS,new PageResult<School>(result.getTotal(),result.getRecords()));

    }

    @CachePut(cacheNames = "school",key = "#school.id")
    public Result save(School school) {
        schoolMapper.insert(school);
        return new Result(ResultCode.SUCCESS,school);
    }

    public Result getCurriculumChildrenBySchool() {
        QueryWrapper<School> schoolQueryWrapper = new QueryWrapper<>();
        schoolQueryWrapper.orderByDesc("sort_number");
        List<School> schools = schoolMapper.selectList(schoolQueryWrapper);
        if(schools != null && schools.size() > 0){
            for (School school : schools) {
                QueryWrapper<Curriculum> curriculumQueryWrapper = new QueryWrapper<>();
                curriculumQueryWrapper.eq("school_id",school.getId());
                curriculumQueryWrapper.orderByDesc("sort_number");
                List<Curriculum> curricula = curriculumMapper.selectList(curriculumQueryWrapper);
                school.setChildren(curricula);
            }
        }
        return new Result(ResultCode.SUCCESS,schools);
    }

    public Result getBooksChildrenBySchool() throws CommonException {
        Result result = this.getCurriculumChildrenBySchool();
        List<School> schools = JSONUtils.ObjectToList(result.getData(), School.class);
        if(schools != null && schools.size() > 0){
            for (School school : schools) {
                List<Curriculum> curricula = school.getChildren();
                if(curricula != null && curricula.size() > 0){
                    for (Curriculum curriculum : curricula) {
                        QueryWrapper<Books> booksQueryWrapper = new QueryWrapper<>();
                        booksQueryWrapper.eq("curriculum_id",curriculum.getId());
                        booksQueryWrapper.orderByDesc("sort_number");
                        List<Books> books = booksMapper.selectList(booksQueryWrapper);
                        curriculum.setChildren(books);
                    }
                }
            }
        }
        return new Result(ResultCode.SUCCESS,schools);
    }


    public Result getChapterChildrenBySchool() throws CommonException {
        Result result = this.getCurriculumChildrenBySchool();
        List<School> schools = JSONUtils.ObjectToList(result.getData(), School.class);
        if(schools != null && schools.size() > 0){
            for (School school : schools) {
                List<Curriculum> curricula = school.getChildren();
                if(curricula != null && curricula.size() > 0){
                    for (Curriculum curriculum : curricula) {
                        QueryWrapper<Books> booksQueryWrapper = new QueryWrapper<>();
                        booksQueryWrapper.eq("curriculum_id",curriculum.getId());
                        booksQueryWrapper.orderByDesc("sort_number");
                        List<Books> books = booksMapper.selectList(booksQueryWrapper);
                        if(books != null && books.size() > 0){
                            for (Books book : books) {
                                QueryWrapper<Chapter> chapterQueryWrapper = new QueryWrapper<>();
                                chapterQueryWrapper.eq("books_id",book.getId());
                                chapterQueryWrapper.orderByDesc("sort_number");
                                List<Chapter> chapters = chapterMapper.selectList(chapterQueryWrapper);
                                book.setChildren(chapters);
                            }
                        }

                        curriculum.setChildren(books);
                    }
                }
            }
        }

        return new Result(ResultCode.SUCCESS,schools);
    }

    public Result getClassesChildrenBySchool() throws CommonException {
        Result result = this.getCurriculumChildrenBySchool();
        List<School> schools = JSONUtils.ObjectToList(result.getData(), School.class);
        if(schools != null && schools.size() > 0){
            for (School school : schools) {
                List<Curriculum> curricula = school.getChildren();
                if(curricula != null && curricula.size() > 0){
                    for (Curriculum curriculum : curricula) {

                        QueryWrapper<Classes> queryWrapper = new QueryWrapper<>();
                        queryWrapper.eq("curriculum_id",curriculum.getId());
                        queryWrapper.orderByAsc("invalid");
                        List<Classes> classes = classesMapper.selectList(queryWrapper);

                        List<Map<String,Object>> list = new ArrayList<>();

                        if(classes != null && classes.size() > 0){
                            for (Classes aClass : classes) {
                                Map<String,Object> map = new HashMap<>();
                                map.put("id",aClass.getId());
                                map.put("name",aClass.getClassesName());
                                map.put("children",null);
                                list.add(map);
                            }
                        }


                        curriculum.setClassesChildren(list);
                    }
                }
            }
        }
        return new Result(ResultCode.SUCCESS,schools);
    }
}
